import router from './router'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

router.beforeEach(async(to, from, next) => {
  // start progress bar
  NProgress.start()

  // set page title
  document.title = getPageTitle(to.meta.title)

  // 处理新窗口打开逻辑
  if((to.path).indexOf("_comp") != -1 ){
    let path = (to.path).replace(/_comp/g,"")
    let url = window.location.protocol + "//" + window.location.host + path;
    if(localStorage.getItem("is_new_window")){
      localStorage.removeItem("is_new_window")
      next(false)
      NProgress.done()
    }else{
      localStorage.setItem("is_new_window",1);
      window.open(url);
      next(false) 
      NProgress.done()
    }
  } else {
    // 直接放行所有路由
    next()
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
